import 'package:flutter/material.dart';

/// 坐标轴线、坐标轴文字
class AxisXY {
  /// 坐标轴线段颜色
  final Color lineColor;

  /// 坐标轴线段宽度
  final double lineWidth;

  /// 坐标轴刻度个数
  final int scaleCount;

  /// 创建坐标轴画笔
  final Paint _axisPaint = Paint()
    ..style = PaintingStyle.stroke
    ..strokeWidth = 1;

  /// 创建x轴、y轴文本画笔
  final TextPainter _textPainter = TextPainter()
    ..textAlign = TextAlign.center
    ..textDirection = TextDirection.ltr;

  AxisXY({
    required this.scaleCount,
    this.lineColor = Colors.black,
    this.lineWidth = 1,
  }) {
    _axisPaint
      ..color = lineColor
      ..style = PaintingStyle.stroke
      ..strokeWidth = lineWidth;
  }

  /// 绘制坐标轴并加刻度
  void drawAxis(Canvas canvas, Size size) {
    /// 坐标轴
    _drawXYCoordAxis(canvas, size);

    /// 刻度
    _drawReticule(canvas, size);
  }

  /// 绘制轴线
  void _drawXYCoordAxis(Canvas canvas, Size size) {
    Path axisPath = Path();

    /// relative相对于上一个点为原点(起始点（0,0）)的位置
    /// 绘制 x 轴线段
    axisPath.relativeLineTo(size.width, 0);
    //  绘制 x 轴箭头
    axisPath.relativeLineTo(-8, 4);
    axisPath.relativeMoveTo(8, -4);
    axisPath.relativeLineTo(-8, -4);

    /// 绘制 y 轴线段
    axisPath.moveTo(0, 0);
    axisPath.relativeLineTo(0, -size.height);
    //  绘制 y 轴箭头
    axisPath.relativeLineTo(-4, 8);
    axisPath.relativeMoveTo(4, -8);
    axisPath.relativeLineTo(4, 8);

    canvas.drawPath(axisPath, _axisPaint);

    /// 添加 x 轴文本
    _textPainter.text = const TextSpan(
      text: 'x轴',
      style: TextStyle(
        fontSize: 12,
        color: Colors.black,
      ),
    );
    _textPainter.layout(); // 进行布局
    _textPainter.paint(canvas, Offset(size.width + _textPainter.width * 0.35, -10)); // 文本绘制尺寸必须在布局后获取

    /// 添加 y 轴文本
    _textPainter.text = const TextSpan(
      text: 'y轴',
      style: TextStyle(
        fontSize: 12,
        color: Colors.black,
      ),
    );
    _textPainter.layout();
    _textPainter.paint(canvas, Offset(-_textPainter.width / 2, -size.height - _textPainter.height - 5));
  }

  /// 绘制坐标轴刻度
  void _drawReticule(Canvas canvas, Size size) {
    double step = size.width / scaleCount;

    for (var i = 1; i < scaleCount; i++) {
      /// x轴刻度文本
      final TextPainter scaleXPainter = TextPainter()
        ..textAlign = TextAlign.end
        ..textDirection = TextDirection.ltr;

      scaleXPainter.text = TextSpan(
        text: '$i',
        style: const TextStyle(
          color: Colors.black,
          fontSize: 12,
        ),
      );
      scaleXPainter.layout();
      scaleXPainter.paint(canvas, Offset(step * i - scaleXPainter.width * 0.5, 5));
      // x轴刻度线 -5 => y的网格-size.height
      canvas.drawLine(Offset(step * i, 0), Offset(step * i, -size.height), _axisPaint);

      /// y轴刻度
      final TextPainter scaleYPainter = TextPainter()
        ..textAlign = TextAlign.left
        ..textDirection = TextDirection.ltr;

      scaleYPainter.text = TextSpan(
        text: '$i',
        style: const TextStyle(
          color: Colors.black,
          fontSize: 12,
        ),
      );
      scaleYPainter.layout();
      scaleYPainter.paint(canvas, Offset(-scaleYPainter.width - 5, -step * i - scaleYPainter.height * 0.5));
      // y轴刻度线 5 => x的网格size.width
      canvas.drawLine(Offset(0, -step * i), Offset(size.width, -step * i), _axisPaint);
    }

    /// (0，0)坐标绘制
    final TextPainter scale0Painter = TextPainter()
      ..textAlign = TextAlign.left
      ..textDirection = TextDirection.ltr;
    scale0Painter.text = const TextSpan(
      text: '0',
      style: TextStyle(
        color: Colors.black,
        fontSize: 12,
      ),
    );
    scale0Painter.layout();
    scale0Painter.paint(canvas, const Offset(-5, 5));
  }
}
